<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'appinclude.php';
require_once 'funcinclude.php';


$link = mysql_connect($_mysql_server, $_mysql_user, $_mysql_password) or die();
mysql_select_db($_mysql_db) or die();


if ($is_app_added==true) { 
	$member = member($user);
} else {
	mysql_close($link);
	echo '<fb:redirect url="'.$appcallbackurl.'">';
	exit;
}

if ($member!=false) {
	if (isset($_REQUEST['club'])) {
		
		$form_club = $_REQUEST['club'];
		$form_position = $_REQUEST['position'];
	
		settype($form_club, "integer");
		settype($form_position, "integer");
	
		$form_name = trim($_REQUEST['name']);
	
		if(get_magic_quotes_gpc()) {
			$form_name = stripslashes($form_name);
		}
		
		if ($form_club!=$member[club_id]) {
			$changes = security($member[changes]);
		} else {
			$changes = $member[changes];
		}
	
		$query = sprintf("UPDATE users SET name='%s', club_id=%d, position=%d, changes=%d WHERE users.fb_id=%d LIMIT 1", 
			mysql_real_escape_string($form_name, $link),
			$form_club,
			$form_position,
			$changes,
			$user);
		
		$result = mysql_query($query) or die();
	
		$member_data_new = member($user);
			
		if ($form_club!=$member[club_id]) {
			$feed = feed('club', $member_data_new);
			$facebook->api_client->feed_publishTemplatizedAction($feed[title_template], $feed[title_data], $feed[body_template], $feed[body_data], '');
		}
		if ($form_position!=$member[position] && $form_position!=9 && $form_position!=10) {
			$feed = feed('position', $member_data_new);
			$facebook->api_client->feed_publishTemplatizedAction($feed[title_template], $feed[title_data], $feed[body_template], $feed[body_data], '');
		}
    	$facebook->api_client->profile_setFBML('', $user, profile($member_data_new), '', '');
    	
		mysql_close($link);
		echo '<fb:redirect url="'.$appcallbackurl.'">';
		exit;
	}
?>

<fb:header decoration="add_border">My <?php echo $_ClubName; ?> Profile</fb:header>

<fb:editor action="edit.php?do-it" labelwidth="100">
  <fb:editor-text label="Name" name="name" value="<?php echo $member[name]; ?>"/>
  <fb:editor-custom label="Club">
    <select name="club">
<?php echo dropdown(clubs(), $member[club_id]); ?>
    </select><br />
   <small> or <a href="<?php echo $appcallbackurl; ?>addclub.php">Add a New Club</a> (Please make sure that yours doesn't exist yet.)</small>
  </fb:editor-custom>
  <fb:editor-custom label="Current Position">
    <select name="position">
<?php echo dropdown($GLOBALS[positions_arr], $member[position]); ?>
    </select>
  </fb:editor-custom>
  <fb:editor-buttonset>
    <fb:editor-button value="Save"/>
    <fb:editor-cancel href="<?php echo $appcallbackurl; ?>" />
  </fb:editor-buttonset>
</fb:editor>

<?
} else {
	echo '<fb:redirect url="'.$appcallbackurl.'">';
}
mysql_close($link);

?>
<fb:google-analytics uacct="<?php echo $_google_analytics_account; ?>" />